package jdbc.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.support.hibernate.DruidConnectionProvider;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.Period;
import java.util.Map;
import java.util.Properties;

/**
 * Druid数据库连接池演示
 */
public class DruidDemo1 {
    public static void main(String[] args) throws Exception {
        //1.导包,导入jar包
        //2.定义配置文件
        //3.加载配置文件
        /*
        数据库的非必须
        Properties prop=new Properties();
        prop.load(new FileInputStream("D:\\idea2022\\day.com.qiezi\\day_12\\druid.properties"));
        //4.获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);*/

        /*
        数据库的必须
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/学生库?useUnicode=true&characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("mysql");*/

        Connection connection = DriverManager.getConnection("jdbc:mysql:///学生库?useSSL=false", "root", "mysql");

        //5.获取数据库连接 Connection
        //Connection connection = dataSource.getConnection();
        System.out.println(connection);
        String sql="select * from tb_brand;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            String sno = resultSet.getString(2);
            //int sname = resultSet.getInt(3);
            //String ssex = resultSet.getString(4);
            System.out.println(sno);
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
